<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
		<link href="css/update_page.css" rel="stylesheet" />
	</head>

	<body>
		<script src="js/mui.js"></script>
		<script type="text/javascript">
			mui.init()
		</script>
		<header class="mui-bar mui-bar-nav my_he">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">更多</h1>
		</header>
		<div class="mui-content">
			<div class="logo">
				<img id="head_img" src=''>
			</div>
			<hr >
			<p id="ban"></p>
			<hr >
			<div class="method">
				<ul class="mui-table-view">
						<li class="mui-table-view-cell">
							<a class="mui-navigate-right" id="check_version">
								检查新版本
							</a>
						</li>
						<li class="mui-table-view-cell" id="version_info">
							<a class="mui-navigate-right">
								新功能介绍
							</a>
						</li>
						<li class="mui-table-view-cell" id="focus_me">
							<a class="mui-navigate-right">
								关于丝雨
							</a>
						</li>
						<li class="mui-table-view-cell" id="help">
							<a class="mui-navigate-right">
								帮助
							</a>
						</li>
					</ul>
			</div>
			<!-- 进度 -->
			<div id="popover" class="mui-popover">
				<div id="ww">
					<div class="heeed">
						下载更新中
					</div>
					<hr >
					<div id="demo1" class="mui-progressbar" style="margin-top: 60px; height: 3px; width: 90%; margin-left: 5%;">
					  	<span></span>
					</div>
					<p style="text-align: center; font-size: 12px;" id="show_num"></p>
					<br>
					<p style="text-align: center; font-size: 12px; margin-top: -10px;">更新中...<br>小主不要按返回键或点击任何按钮 否则会导致更新失败!</p>
				</div>
			</div>
			
		</div>
		
		<script type="text/javascript">
			mui.plusReady(function(){
			    mui.getJSON("manifest.json", null, function(manifest){
			        versionName = manifest.version.name;
			        versionCode = Number(manifest.version.code);
					document.getElementById('ban').innerHTML = "版本号："+versionName+"，版本code："+versionCode
					document.getElementsByClassName('logo')[0].firstElementChild.src = localStorage.getItem('request_url') + 'static/my_image/update_logo.png'
				});
			});
			
			// 关于我
			document.getElementById('focus_me').addEventListener('tap', function(){
				var my_info = '随风潜入夜 润物细无声\n\n感谢您对本软件的支持\n\n我很懒，请提醒我及时更新'
				mui.alert(my_info, '丝雨', function() {
					mui.toast('感谢您的支持')
				});
			})
			
			// 帮助
			document.getElementById('help').addEventListener('tap', function(){
				mui.openWindow({
					url: 'help_page.html',
					id: 'help_page.html',
				})
			})
			
			
			
			// 查看热点， 应该根据版本来取得功能描述
			document.getElementById('version_info').addEventListener('tap',function(){
				mui.ajax(localStorage.getItem('request_url')+'cost/get_version_info/',{
				    data:"token=" + localStorage.getItem('uid')+ "&version_code=" + versionCode,
				    dataType: 'json',
				    type:'post',  
				    contentType:"application/x-www-form-urlencoded; charset=utf-8",  
				    timeout:60000,  
				    success:function show(e){
						mui.alert(e.data.version_info, '版本新增', function() {
							mui.toast('感谢您的支持')
						});
					},
				    error:function(xhr,type,errorThrown){
				        mui.toast('未知错误')
					}
				});
			})
			
			// 检查更新
			document.getElementById('check_version').addEventListener('tap',function(){
				
				mui.ajax(localStorage.getItem('request_url')+'cost/check_code/',{
				    data:"token=" + localStorage.getItem('uid'),
				    dataType: 'json',
				    type:'post',  
				    contentType:"application/x-www-form-urlencoded; charset=utf-8",  
				    timeout:60000,  
				    success:function show(e){
						var new_versonName = e.data.versionName
						var new_versonCode = e.data.versionCode
						if(new_versonName != versionName | new_versonCode != versionCode){
							// 发现新版本，请求新版本信息
							mui.ajax(localStorage.getItem('request_url')+'cost/get_version_info/',{
							    data:"token=" + localStorage.getItem('uid')+ "&version_code=" + new_versonCode,
							    dataType: 'json',
							    type:'post',  
							    contentType:"application/x-www-form-urlencoded; charset=utf-8",  
							    timeout:60000,  
							    success:function show(e){
									var btnArray = ['以后再说', '立即更新'];
									mui.confirm(e.data.version_info, '发现新版本', btnArray, function(e) {
										if(e.index == 1) {
											dowload()
										}else{
											mui.toast('取消更新')
										}
									});
								},
							    error:function(xhr,type,errorThrown){
							        mui.toast('未知错误')
								}
							});
						}else{
							mui.alert('已是最新版本，无需更新', '小主', function() {
								mui.toast('感谢您的支持')
							});
						}
					},
				    error:function(xhr,type,errorThrown){
				        mui.toast('完蛋，未知错误...')
					}
				});
			});
			
			
			
			
			
			// 下载更新
			function dowload(){
				
				mui('.mui-popover').popover('toggle',);
				var url = localStorage.getItem('request_url') + 'static/code/new.apk';
				plus.nativeUI.toast("正在准备环境，请稍后！");
				// 展示进度条
				// document.getElementById('jindu').style.display = 'block';
				// 初始化进度条
				mui("#demo1").progressbar().show();
				
				
				var dtask = plus.downloader.createDownload(url ,{},function(dowload,status){
					if(status == 200){
						var path = dowload.filename;//下载apk
						plus.runtime.install(path); // 自动安装apk文件
					}else{
						plus.nativeUI.alert('版本更新失败:' + status);
					}
				});
				
				// 监听下载
				dtask.addEventListener("statechanged", function (download, status) {
					switch(download.state){
						case 3:
							var totalSize = dtask.totalSize
							var downloadedSize = dtask.downloadedSize
							document.getElementById('show_num').innerText = '已下载:   ' + (downloadedSize/totalSize*100).toFixed(2) + '%'
							// 设置显示进度
							mui("#demo1").progressbar().setProgress((downloadedSize/totalSize*100).toFixed(2));
						break;
						case 4:
							//隐藏进度条 
							document.getElementById('jindu').style.display = 'none';
							mui.toast("下载完成");
							break;
					}
				});
				
				setTimeout(function(){
					dtask.start();
				},200)
				
			};
		</script>
	</body>

</html>
